<?php
namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use EasyWeChat\Factory;
use EasyWeChat\Kernel\Messages\Message;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use App\Models\Cart;
use Log;

class CartController extends Controller
{

    /**
     * @api {post} /cart/list
     * 购物车列表
     */
    public function list(Request $request)
    {
        $rules = [
            'page'            => 'integer|min:1',
            'per_page'        => 'required_with:page|integer|min:1',
        ];

        if ($error = $this->validateInput($rules)) {
            return $error;
        }

        $member =  \Auth::guard('member')->user();

        $this->validated['member_id'] = $member->member_id;

        $data = Cart::getList($this->validated);

        return $this->output(200, $data);
    }

    /**
     * @api {post} /cart/add
     * 添加/编辑购物车
     */
    public function add(Request $request)
    {
        $rules = [
            'goods_id'          => 'required|integer|min:1',
            'sku_id'            => 'required|integer|min:1',
            'num'               => 'required|integer|min:1',
        ];

        if ($error = $this->validateInput($rules)) {
            return $error;
        }

        $member =  \Auth::guard('member')->user();

        $this->validated['member_id'] = $member->member_id;

        $data = Cart::add($this->validated);

        return $this->output(200, $data);
    }

    /**
     * @api {post} /cart/delete
     * 添加/编辑购物车
     */
    public function delete(Request $request)
    {
        $rules = [
            'cart_id'          => 'required',
        ];

        if ($error = $this->validateInput($rules)) {
            return $error;
        }

        $member =  \Auth::guard('member')->user();

        $this->validated['member_id'] = $member->member_id;

        $data = Cart::deleteCart($this->validated);

        return $this->output(200, $data);
    }

    /**
     * @api {post} /cart/count
     * 获取购物车数量
     */
    public function count(Request $request)
    {
        $member =  \Auth::guard('member')->user();

        $this->validated['member_id'] = $member->member_id;

        $data = Cart::countGoodsNum($this->validated);

        return $this->output(200, $data);
    }

}
